import RenderCell from 'packages/BaseTableCell/renderCell.js';
import formatColumn from 'packages/BaseTableCell/formatColumn';
import { formatDate } from 'pub-bbx-utils';
import { isOpenData } from '@src/util/platform';
import {atTextToHtml1} from '@src/util/atText'
import i18n from '@src/locales'
import { SessionSourceMap } from '@model/enum/ImEnum.js'

const registerTypeMaps = {
  2: i18n.t('im.list.registerTypeMaps.label1'),
  3: i18n.t('im.list.registerTypeMaps.label2'),
  4: i18n.t('im.list.registerTypeMaps.label3')
}
const genderMaps = {
  0: i18n.t('common.base.woman'),
  1: i18n.t('common.base.man')
}
// 高级搜索
export const getAdvancedFields = (
  vm,
  { AllPutChannel = [], AllServiceNumber = [], AllConsultTypeList = [] }
) => {
  let that = vm;
  return [
    {
      fieldName: 'customer', // customerId
      displayName: i18n.t('common.form.type.customerName'),
      formType: 'text',
    },
    {
      fieldName: 'linkmanId',
      displayName: i18n.t('common.fields.contact.displayName'),
      formType: 'select',
    },
    {
      fieldName: 'customerServiceNumber',
      displayName: i18n.t('common.fields.customerService.displayName'),
      formType: 'select',
      setting: {
        isMulti: false,
        dataSource: AllServiceNumber,
      },
    },
    {
      fieldName: 'relationUserId',
      displayName: i18n.t('common.fields.relationUserId.displayName'),
      formType: 'user',
      setting: {
        isMulti: false,
      },
      defineType: '',
    },
    {
      fieldName: 'imStartTime',
      displayName: i18n.t('common.fields.imStartTime.displayName'),
      formType: 'datetime',
      setting: {
        toString: true,
      },
    },
    {
      fieldName: 'imEndTime',
      displayName: i18n.t('common.fields.imEndTime.displayName'),
      formType: 'datetime',
      setting: {
        toString: true,
      },
    },
    {
      fieldName: 'putChannelId',
      displayName: i18n.t('common.fields.imPutChannelId.displayName'),
      formType: 'select',
      setting: {
        isMulti: false,
        dataSource: AllPutChannel,
      },
    },
    {
      fieldName: 'consultName',
      displayName: i18n.t('common.fields.imConsultName.displayName'),
      formType: 'cascader',
      setting: {
        isMulti: false,
        dataSource: AllConsultTypeList,
      },
    },
    {
      fieldName: 'eventNo',
      displayName: i18n.t('common.fields.eventContact.displayName'),
      formType: 'text',
    },
    {
      fieldName: 'registerType',
      displayName: i18n.t('common.fields.imRegisterType.displayName'),
      formType: 'select',
      setting: {
        isMulti: false,
        dataSource: [
          {
            text: i18n.t('common.fields.imRegisterType.dataSource.text1'),
            value: 2,
          },
          {
            text: i18n.t('common.fields.imRegisterType.dataSource.text2'),
            value: 3,
          },
        ],
      },
    },
    {
      fieldName: 'productNo',
      displayName: i18n.t('common.fields.productNo.displayName'),
      formType: 'text',
      placeHolder: i18n.t('common.fields.productNo.placeholder'),
    },
    {
      fieldName: 'imCloseTime',
      displayName: i18n.t('common.fields.imCloseTime.displayName'),
      formType: 'datetime',
      setting: {
        toString: true,
      },
    },
  ].map(item => {
    return {
      ...item,
      setting: item.setting || {},
      defaultValue: null,
      show: true,
      isSearch: 1,
      isNull: 1,
      isSystem: 1,
      orderId: 1,
    };
  });
};

// 列表 && 选择列
export const getColumnFields = vm => {
  const that = vm;

  return [
    {
      fieldName: 'conversationNumber',
      displayName: i18n.t('common.fields.imConversationNumber.displayName'),
      sortName: 'conversationNumber',
      dataType: 'string',
      renderCell: (h, col, row) => {
        return RenderCell.renderIm.call(that, h, row, that.allowImView);
      },
    },
    {
      fieldName: 'customerName',
      displayName: i18n.t('common.fields.customerName.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.customerName}</div>;
      },
    },
    {
      fieldName: 'linkmanName',
      displayName: i18n.t('common.fields.contact.displayName'),
      renderCell: (h, col, row) => {
        if (row.source == 'weChat' && row.linkmanName) {
          return (
            <div>
              {row.linkmanName}
              <span class="wx-im iconfont icon-weixin5"></span>
            </div>
          );
        }
        return <div>{row.linkmanName}</div>;
      },
    },
    {
      fieldName: 'email',
      displayName: i18n.t('common.fields.email.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{ row.email }</div>;
      },
    },
    {
      fieldName: 'registerType',
      displayName: i18n.t('common.fields.registerType.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{registerTypeMaps[row.registerType]}</div>;
      },
    },
    {
      fieldName: 'touristNo',
      displayName: i18n.t('common.fields.touristNo.displayName'),
      renderCell: (h, col, row) => {
        return <div>{row.touristNo}</div>;
      },
    },
    {
      fieldName: 'phoneNumber',
      displayName: i18n.t('common.fields.phone.displayName'),
      renderCell: (h, col, row) => {
        return <div>{row.phoneNumber}</div>;
      },
    },
    {
      fieldName: 'address',
      displayName: i18n.t('common.fields.address.displayName'),
      width: 210,
      renderCell: (h, col, row) => {
        return <div>{row.address}</div>;
      },
    },
    {
      fieldName: 'customerServiceNumber',
      displayName: i18n.t('common.fields.customerServiceNumber.displayName'),
      sortName: 'customerServiceNumber',
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.customerServiceNumber}</div>;
      },
    },
    {
      fieldName: 'customerServiceName',
      displayName: i18n.t('common.fields.customerServiceName.displayName'),
      sortName: 'customerServiceName',
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.customerServiceName}</div>;
      },
    },
    {
      fieldName: 'relationUserId',
      displayName: i18n.t('common.fields.relationUserId.displayName'),
      sortName: 'relationUserId',
      dataType: 'string',
      width: 210,
      renderCell: (h, col, row) => {
        if (isOpenData) {
          return (
            <div>
              {<open-data type="userName" openid={row.relationUserStaffId} />}
            </div>
          );
        }
        return <div>{row.relationUserName}</div>;
      },
    },
    {
      fieldName: 'startTime',
      displayName: i18n.t('common.fields.imStartTime.displayName'),
      sortable: 'custom',
      sortName: 'startTime',
      dataType: 'date',
      renderCell: (h, col, row) => {
        return <div>{formatDate(row.startTime, 'YYYY-MM-DD HH:mm:ss')}</div>;
      },
    },
    {
      fieldName: 'endTime',
      displayName: i18n.t('common.fields.imEndTime.displayName'),
      sortable: 'custom',
      sortName: 'endTime',
      dataType: 'date',
      renderCell: (h, col, row) => {
        return <div>{formatDate(row.endTime, 'YYYY-MM-DD HH:mm:ss')}</div>;
      },
    },
    {
      fieldName: 'source',
      displayName: i18n.t('common.fields.imSource.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{ SessionSourceMap[row.source] || '' }</div>;
      },
    },
    {
      fieldName: 'putChannelName',
      displayName: i18n.t('common.fields.imPutChannelId.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return (
          <div>{row.putChannelName ? row.putChannelName.join(',') : ''}</div>
        );
      },
    },
    {
      fieldName: 'conversationStatus',
      displayName: i18n.t('common.fields.conversationStatus.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return (
          <div>
            {
              [i18n.t('common.fields.conversationStatus.datasource.text1'), i18n.t('common.fields.conversationStatus.datasource.text2'),i18n.t('common.fields.conversationStatus.datasource.text3'),i18n.t('common.fields.conversationStatus.datasource.text4')][
                row.conversationStatus
              ]
            }
          </div>
        );
      },
    },
    {
      fieldName: 'consultName',
      displayName: i18n.t('common.fields.imConsultName.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.consultName}</div>;
      },
    },
    {
      fieldName: 'solveStatus',
      displayName: i18n.t('common.fields.solveStatus.displayName'),
      dataType: 'string',
    },
    {
      fieldName: 'eventNo',
      displayName: i18n.t('common.fields.eventContact.displayName'),
      sortName: 'eventNo',
      dataType: 'string',
    },
    // {
    //   fieldName: 'taskNo',
    //   displayName: '工单编号',
    //   dataType: 'string',
    //   renderCell: (h, col, row) => {
    //     return <div>{row.taskNo}</div>;
    //   },
    // },
    {
      fieldName: 'bizRelTaskInfo',
      displayName: i18n.t('common.fields.relationTask.displayName'),
      dataType: 'string',
    },
    {
      fieldName: 'productNo',
      displayName: i18n.t('common.fields.productNo.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.productNo}</div>;
      },
    },
    {
      fieldName: 'bizRelProductInfo',
      displayName: i18n.t('common.fields.bizRelProductInfo.displayName'),
      dataType: 'string',
    },
    {
      fieldName: 'responseTime',
      displayName: i18n.t('common.fields.imResponseTime.displayName'),
      sortable: 'custom',
      sortName: 'responseTime',
      dataType: 'date',
      renderCell: (h, col, row) => {
        return <div>{formatDate(row.responseTime, 'YYYY-MM-DD HH:mm:ss')}</div>;
      },
    },
    {
      fieldName: 'closeTime',
      displayName: i18n.t('common.fields.imCloseTime.displayName'),
      sortable: 'custom',
      sortName: 'closeTime',
      dataType: 'date',
      renderCell: (h, col, row) => {
        return <div>{formatDate(row.closeTime, 'YYYY-MM-DD HH:mm:ss')}</div>;
      },
    },
    {
      fieldName: 'responseDuration',
      displayName: i18n.t('common.fields.imResponseDuration.displayName'),
      dataType: 'string',

    },
    {
      fieldName: 'robotReception',
      displayName: i18n.t('common.fields.robotReception.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.robotReception ? i18n.t('common.base.yes') : i18n.t('common.base.no')}</div>;
      },
    },
    {
      fieldName: 'artificialReception',
      displayName: i18n.t('common.fields.artificialReception.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.artificialReception ? i18n.t('common.base.yes') : i18n.t('common.base.no')}</div>;
      },
    },
    {
      fieldName: 'isVideo',
      displayName: i18n.t('common.fields.isVideo.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.isVideo ? i18n.t('common.base.yes') : i18n.t('common.base.no')}</div>;
      },
    },
    {
      fieldName: 'serviceRemark',
      displayName: i18n.t('common.fields.serviceRemark.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{atTextToHtml1(row.serviceRemark)}</div>;
      },
    },
    {
      fieldName: 'nickname',
      displayName: i18n.t('common.fields.weChatNickname.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.nickname}</div>;
      },
    },
    {
      fieldName: 'weChatId',
      displayName: i18n.t('common.fields.weChatId2.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.weChatId}</div>;
      },
    },
    {
      fieldName: 'isTimeOut',
      displayName: i18n.t('common.fields.isTimeOut.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.isTimeOut ? i18n.t('common.base.yes') : i18n.t('common.base.no')}</div>;
      },
    },
    {
      fieldName: 'gender',
      displayName: i18n.t('common.fields.sex.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        if(row.source == 'weChat') return <div>{i18n.t('common.base.unknown')}</div>
        return <div>{genderMaps[row.gender]}</div>;
      },
    },
  ].map(item => {
    // 初始化属性
    let field = {
      ...item,
      formType: 'text',
      setting: item.setting || {},
      isNull: 1,
      isSearch: 1,
      orderId: 1,
      show: true,
      tableName: 'im',
      minWidth: 150,
      visable: typeof item.visable == 'undefined' ? true : item.visable,
      isSystem:1
    };
    let formatField = formatColumn(field);
    if(['responseTime','closeTime'].includes(formatField.fieldName)) formatField.sortable = false
    return formatField
  });
};

export const getExportSystemInfo = () => {
  return [
    {
      exportAlias: 'conversationNumber',
      displayName: i18n.t('common.fields.imConversationNumber.displayName'),
    },
    {
      exportAlias: 'customerName', // customerId
      displayName: i18n.t('common.fields.customerName.displayName'),
    },
    {
      exportAlias: 'linkmanName',
      displayName: i18n.t('common.fields.contact.displayName'),
    },
    {
      exportAlias: 'phoneNumber',
      displayName: i18n.t('common.fields.phone.displayName'),
    },
    {
      exportAlias: 'email',
      displayName: i18n.t('common.fields.email.displayName'),
    },
    {
      exportAlias: 'address',
      displayName: i18n.t('common.fields.address.displayName'),
    },
    {
      exportAlias: 'customerServiceNumber',
      displayName: i18n.t('common.fields.customerServiceNumber.displayName'),
    },
    {
      exportAlias: 'customerServiceName',
      displayName: i18n.t('common.fields.customerServiceName.displayName'),
    },
    {
      exportAlias: 'relationUserId',
      displayName: i18n.t('common.fields.relationUserId.displayName'),
    },
    {
      exportAlias: 'imStartTime',
      displayName: i18n.t('common.fields.imStartTime.displayName'),
    },
    {
      exportAlias: 'imEndTime',
      displayName: i18n.t('common.fields.imEndTime.displayName'),
    },
    {
      exportAlias: 'source',
      displayName: i18n.t('common.fields.imSource.displayName'),
    },
    {
      exportAlias: 'putChannel',
      displayName: i18n.t('common.fields.imPutChannelId.displayName'),
    },
    {
      exportAlias: 'conversationStatus',
      displayName: i18n.t('common.fields.conversationStatus.displayName'),
    },
    {
      exportAlias: 'consultName',
      displayName: i18n.t('common.fields.imConsultName.displayName'),
    },
    {
      exportAlias: 'solveStatus',
      displayName: i18n.t('common.fields.solveStatus.displayName'),
    },
    {
      exportAlias: 'eventNo',
      displayName: i18n.t('common.fields.eventContact.displayName'),
    },
    {
      exportAlias: 'registerType',
      displayName: i18n.t('common.fields.imRegisterType.displayName'),
    },
    // {
    //   exportAlias: 'taskNo',
    //   displayName: '工单编号',
    // },
    {
      exportAlias: 'productNo',
      displayName: i18n.t('common.fields.productNo.displayName'),
    },
    {
      exportAlias: 'responseTime',
      displayName: i18n.t('common.fields.imResponseTime.displayName'),
    },
    {
      exportAlias: 'closeTime',
      displayName: i18n.t('common.fields.imCloseTime.displayName'),
    },
    {
      exportAlias: 'responseDuration',
      displayName: i18n.t('common.fields.imResponseDuration.displayName'),
    },
    {
      exportAlias: 'robotReception',
      displayName: i18n.t('common.fields.robotReception.displayName'),
    },
    {
      exportAlias: 'artificialReception',
      displayName: i18n.t('common.fields.artificialReception.displayName'),
    },
    {
      exportAlias: 'isVideo',
      displayName: i18n.t('common.fields.isVideo.displayName'),
    },
    {
      exportAlias: 'serviceRemark',
      displayName: i18n.t('common.fields.serviceRemark.displayName'),
    },
    {
      exportAlias: 'nickname',
      displayName: i18n.t('common.fields.weChatNickname.displayName'),
    },
    {
      exportAlias: 'gender',
      displayName: i18n.t('common.fields.sex.displayName'),
    },
    {
      exportAlias: 'weChatId',
      displayName: i18n.t('common.fields.weChatId2.displayName'),
    },
    {
      exportAlias: 'bizRelProductInfo',
      displayName: i18n.t('common.fields.bizRelProductInfo.displayName'),
    },
    // 关联工单导出taskNo字段
    {
      exportAlias: 'taskNo',
      displayName: i18n.t('common.fields.relationTask.displayName'),
    },
    {
      exportAlias: 'touristNo',
      displayName: i18n.t('common.fields.touristNo.displayName'),
    },
  ];
};
